package com.qf.automation.sms.nacos;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.qf.automation.sms.inf.SMSInf;
import com.qf.automation.sms.service.impl.SMSServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.Executor;

@Component
public class NacosDynamicService {

    @Value("${spring.cloud.nacos.config.server-addr}")
    private String serverAddr;

    @Resource
    private SMSServiceImpl smsService;

    //在nacos上的配置文件的坐标(dataId,group)
    private String dataId="sms-config";
    private String group="DEFAULT_GROUP";
    /**
     * 获取且监听nacos上的配置信息
     */
    @PostConstruct
    public void dynamicByNacosListener(){
        try {
            //配置服务： 用于获取、监听  nacos上的配置信息
            ConfigService configService = NacosFactory.createConfigService(serverAddr);
            //根据nacos坐标获取nacos上的配置文件，超时时间为5000ms
            String config=configService.getConfig(dataId,group,5000);
            JSONObject configObject=JSONObject.parseObject(config);
            String type=configObject.getString("type");
            smsService.setExecutor((SMSInf)Class.forName(type).newInstance());
          /*  if("baidu".equalsIgnoreCase(type)){
                smsService.setExecutor(new BaiduSender());
            }else if("alibaba".equalsIgnoreCase(type)){
                smsService.setExecutor(new AlibabaSender());
            }else if("yunpian".equalsIgnoreCase(type)){
                smsService.setExecutor(new YunPianSender());
            }*/


            //监听逻辑
            configService.addListener(dataId, group, new Listener() {
                /**
                 * 获取执行者的方法，空实现就可以。
                 * @return
                 */
                @Override
                public Executor getExecutor() {
                    return null;
                }

                /**
                 * 接收配置信息
                 * 也就是说，当配置信息发生变化之时，nacos config客户端用
                 * 这个方法获取到最新的配置信息
                 * @param config
                 */
                @Override
                public void receiveConfigInfo(String config) {
                    JSONObject configObject=JSONObject.parseObject(config);
                    String type=configObject.getString("type");
                    try {
                        smsService.setExecutor((SMSInf)Class.forName(type).newInstance());
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    /*if("baidu".equalsIgnoreCase(type)){
                        smsService.setExecutor(new BaiduSender());
                    }else if("alibaba".equalsIgnoreCase(type)){
                        smsService.setExecutor(new AlibabaSender());
                    }else if("yunpian".equalsIgnoreCase(type)){
                        smsService.setExecutor(new YunPianSender());
                    }*/
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
